package com.testing.inter;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.testing.DriverSelf.HttpDriver;
import com.testing.common.AutoTools;
import jdk.nashorn.internal.ir.ReturnNode;

import java.rmi.registry.Registry;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Classname InterKeyWord
 * @Description 类型说明
 * @Date 2022/11/2 22:19
 * @Created by 特斯汀Roy
 */
public class InterKeyWord extends AutoTools {

    public HttpDriver driver;

    //接口的返回信息
    public String result;

    public String getResult() {
        return this.result;
    }

    public InterKeyWord() {
        driver = new HttpDriver();
    }

    public boolean useCookie() {
        driver.useCookie();
        return true;
    }

    public boolean notUseCookie() {
        driver.notUseCookie();
        return true;
    }

    public boolean useHeader() {
        driver.useHeader();
        return true;
    }

    public boolean notUseHeader() {
        driver.notUseHeader();
        return true;
    }

    /**
     * 添加头的时候，用json格式  {“键”:"值"，“键”:"值"}
     *
     * @param headerJson
     */
    public boolean addHeader(String headerJson) {
        driver.addHeader(useParam(headerJson));
        return true;
    }

    public boolean clearHeader() {
        driver.clearHeader();
        return true;
    }

    /***
     * post请求发包
     */
    public void testPostUrl(String url, String param) {
        result = driver.doPostUrl(useParam(url), useParam(param));
        System.out.println("输出请求的返回结果是：" + result);
    }

    public void testGet(String url) {
        result = driver.doGet(useParam(url));
    }

    public void testPostJson(String url, String param) {
        result = driver.doPostJson(useParam(url), useParam(param));
    }

    public void testPostFile(String url, String param) {
        result = driver.doPostFile(useParam(url), useParam(param));
    }

    public void testPostXml(String url, String param) {
        result = driver.doPostXml(useParam(url), useParam(param));
        System.out.println(result);
    }

    public void testPutUrl(String url, String param) {
        result = driver.doPutUrl(useParam(url), useParam(param));
    }

    public void testDelete(String url) {
        result = driver.doDelete(useParam(url));
    }


    /****
     * 参数关联方法，存储通过json、 正则获取的参数
     */

    public boolean saveJsonParam(String paramName, String jsonPath) {
        String jsonValue;
        try {
            jsonValue = JSONPath.read(result, jsonPath).toString();
            saveParam(paramName, jsonValue);
            return true;
        } catch (Exception e) {
            log.error("json字符串解析失败", e.fillInStackTrace());
            saveParam(paramName, e.fillInStackTrace().toString());
            return false;
        }
    }

    /**
     * 基于外部传参来进行jsonPath解析，完成参数存储。
     * @param paramName
     * @param origin
     * @param jsonPath
     */
//    public void saveJsonParam(String paramName,String origin,String jsonPath){
//        String jsonValue;
//        try {
//            jsonValue = JSONPath.read(origin, jsonPath).toString();
//            saveParam(paramName, jsonValue);
//        } catch (Exception e) {
//            log.error("json字符串解析失败",e.fillInStackTrace());
//            saveParam(paramName, e.fillInStackTrace().toString());
//        }
//    }

    /**
     *
     * @param paramName
     * @param regex
     * @return
     */
//    public String saveRegexReqParam(String paramName,String regex){
//        Pattern p = Pattern.compile(regex);
//        Matcher matcher = p.matcher(result);
//        if(matcher.find()){
//            log.info("正则表达式"+regex+"获取的结果是"+matcher.group(1));
//            saveParam(paramName,matcher.group(1));
//            return matcher.group(1);
//        }
//        else{
//            saveParam(paramName,"没找到匹配的内容");
//            return "没找到匹配的内容";
//        }
//
//
//    }


    /**
     * 正则最好写成  xxx(.*？)xxx的格式
     *
     * @param paramName
     * @param regex
     */
    public boolean saveRegexParam(String paramName, String regex) {
        try {
            Pattern compile = Pattern.compile(regex);
            Matcher m = compile.matcher(result);
            String regexValue = "正则没有匹配到";
            if (m.find()) {
                regexValue = m.group(1);
            }
            saveParam(paramName, regexValue);
            return true;
        } catch (Exception e) {
            saveParam(paramName, e.fillInStackTrace().toString());
            return false;
        }
    }


    /***
     * 断言
     */
    public boolean assertJsonValue(String jsonPath, String expectedResult) {
        jsonPath = useParam(jsonPath);
        expectedResult = useParam(expectedResult);
        String actual = JSONPath.read(result, jsonPath).toString();
        if (expectedResult.equals(actual)) {
            AutoTools.log.info("测试成功实际值是:" + actual);
            return true;
        } else {
            AutoTools.log.error("测试失败实际值是" + actual);
            return false;
        }
    }

    /**
     * 断言json字段包含内容
     *
     * @param expectedResult
     * @param jsonPath
     * @return
     */
    public boolean assertJsonContainsValue(String jsonPath, String expectedResult) {
        jsonPath = useParam(jsonPath);
        expectedResult = useParam(expectedResult);
        String actual = JSONPath.read(result, jsonPath).toString();
        if (actual.contains(expectedResult)) {
            AutoTools.log.info("测试成功实际值是:" + actual);
            return true;
        } else {
            AutoTools.log.error("测试失败实际值是" + actual);
            return false;
        }
    }

    /**
     * 通过正则表达式提取信息完成断言
     *
     * @param regex
     * @param expectedResult
     * @return
     */
    public boolean assertRegexValue(String regex, String expectedResult) {
        //因为一般不会出现数字作为参数名，不会和正则的{1}量词冲突。
        regex = useParam(regex);
        expectedResult = useParam(expectedResult);
        Pattern compile = Pattern.compile(regex);
        Matcher m = compile.matcher(result);
        String actual = "正则没有匹配到";
        if (m.find()) {
            actual = m.group(1);
        }
        if (actual.equals(expectedResult)) {
            log.info("测试成功");
            return true;
        } else {
            log.error("测试失败，实际值是" + actual);
            return false;
        }


    }

}
